import 'package:flutter_common/ui/dialog/picker_dialog.dart';
import 'package:example/view/loading_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter_common/ui/dialog/dialog.dart';
import 'package:flutter_common/ui/dialog/loading_dialog.dart';
import 'package:flutter_common/ui/dialog/popup_menu_dialog.dart';
import 'package:flutter_common/ui/dialog/select_bottom_sheet.dart';
import 'package:flutter_common/ui/scaffold.dart';
import 'package:flutter_common/ui/widget/button.dart';
import 'package:flutter_common/ui/widget/popup_menu_button.dart';
import 'package:flutter_common/ui/widget/skeleton.dart';
import 'package:flutter_common/util/logger.dart';
import 'package:flutter_common/util/toast_util.dart';

class DialogDemo extends StatelessWidget with LoadingDataSkeleton<int> {

  Future<int> _dd() {
    return Future.delayed(const Duration(seconds: 1)).then((value) => 1);
  }

  @override
  Widget build(BuildContext context) {
    return FxScaffold(
      appBar: FxAppBar(title: 'dialog demo', actions: [
        Builder(builder: (context){
          return FxOutlinedButton(
            'menu',
            onPressed: () {
              showPopupMenu(
                context,
                offset: const Offset(0,40),
                builder: (context) => _popupItemList(),
                onSelected: (value) {
                  ToastUtil.showToast('show popup menu ${value.value}');
                },
              );
            },
          );
        }),
        FxPopupMenuButton<MoreItem>(
          builder: (context) => _popupItemList(),
          onSelected: (value) {
            ToastUtil.showToast('show popup menu ${value.value}');
          },
        ),
      ]),
        body:
            FutureBuilder(future: _dd(), initialData: 0, builder: loadingData),
        );
  }

  @override
  Widget doneWidget(BuildContext context, data) {
    return Center(
        child: SingleChildScrollView(
      child: Column(children: [
        FxOutlinedButton(
          "alert dialog",
          onPressed: () {
            showAlertDialog(context,
                "MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMDDDD",
                outsideCancel: false);
          }
        ),
        Container(height: 20),
        TextButton(
            onPressed: () {
              showConfirmDialog(context, "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG",
                  onOkPressed: () {
                ToastUtil.successToast("click ok button");
              }, okText: "删除删除", title: "温馨提示");
            },
            child: const Text("confirm dialog")
        ),
        Container(height: 20),
        FxOutlinedButton("input dialog", onPressed: () {
          showInputDialog(context,
              title: "温馨提示",
              emptyInput: false,
              tips: "tipstips",
              onOkPressed: (s) => ToastUtil.showToast(s));
        }),
        Container(height: 20),
        FxOutlinedButton("loading dialog", onPressed: () {
          showLoadingDialog(context, future: () async {
            print("show loading future");
            await Future.delayed(const Duration(seconds: 10));
            return 1;
          }(), outsideCancel: true);
        }),
        LoadingView(),
        FxElevatedButton('bottom dialog', onPressed: (){
          //showBottomSheet 命名冲突，修改方法名
          showSelectBottomSheet<String, String, List<String>>(
            context,
            //字符串比较，可以不传这个
            equals: (select, item) => select == item,
            //字符串 或者有toString，可以不传
            optionMapper: (item) => item,
            //多选
            multi: false,
            title: "标题",
            list: ["1", "1", "10", "温馨提示温馨提示温馨提示温馨提示温馨提示温馨提示"],
            selectList: ["2"],
            onOkPress: (res) {
              ToastUtil.showToast("bottom dialog: ${res.join(',')}");
            },
          );
          // ToastUtil.showToast("bottom dialog");
        }),
        Container(height: 20),
        FxElevatedButton('right dialog', onPressed: (){
          showDialog(
            context: context,
            useSafeArea: false,
            builder: (context) {
              return FxSelectListDialog<String, String, List<String>>(
                multi: true,
                list: const ["1", "1", "1", "1", "1", "1", "1", "1", "2","3","4","5","6","7","8","9","10", "温馨提示温馨提示温馨提示温馨提示温馨提示温馨提示"],
                selectList: const ["2"],
                alignment: Alignment.centerRight,
                width: null,
                height: double.infinity,
                borderRadius: const BorderRadius.horizontal(left: Radius.circular(10)),
                equals: (select, item) => select == item,
                optionMapper: (item) => item,
                onOkPress: (res) {
                  ToastUtil.showToast("right dialog: ${res.join(',')}");
                },
              );
            },
          );
          // ToastUtil.showToast("bottom dialog");
        }),
        Container(height: 20),
        FxElevatedButton('input dialog', onPressed: () => _onInputDialog(context)),
        Container(height: 20),
        FxElevatedButton('timer dialog', onPressed: () {
          PickerDialog.showDatePicker(context, dateType: DateType.YMD_AP_HM, clickCallback: (res, b){
            logger.error(res, b);
          });
        }),
      ]),
    ));
  }

  List<FxPopupItem<MoreItem>> _popupItemList(){
    return [
      FxPopupItem(
        assetsIcon: const Icon(Icons.add, size: 15,color: Colors.black45,),
        value: MoreItem(value: "1"),
        name: "上报审批",
      ),
      FxPopupItem(
        assetsIcon: const Icon(Icons.ac_unit, size: 15,color: Colors.black45,),
        value: MoreItem(value: "2"),
        name: "连线公证员",
      ),
      FxPopupItem(
        assetsIcon: const Icon(Icons.access_alarm, size: 15,color: Colors.black45,),
        value: MoreItem(value: "3"),
        name: "公证人员签署",
      ),
      FxPopupItem(
        assetsIcon: const Icon(Icons.accessibility, size: 15,color: Colors.black45,),
        value: MoreItem(value: "4"),
        name: "作废",
      ),
    ];
  }

  void _onInputDialog(BuildContext context){
    showInputDialog(context,
        title: '不通过提示',
        subTitle: "审核不通过后，该文件将只保留申请记录",
        hint: '请输入不通过理由，非必填，限30汉字',
        maxLine: 5,
        okText: '拒绝',
        maxLength: 30,
        onOkPressed: (v) {});
  }
}

class MoreItem{
  MoreItem({this.value});
  String? value;

}